<template>
  <div>
    <el-button @click="addArea">添加</el-button>
    <div class="table">
      <el-table
        :data="areaList"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="序号"
          width="180"
        />
        <el-table-column
          prop="areaName"
          label="区域名称"
          width="180"
        />
        <el-table-column label="车位数(个)" prop="spaceNumber" />
        <el-table-column label="面积(M²)" prop="areaProportion" />
        <el-table-column label="计费规则" prop="ruleName" />
        <el-table-column label="备注" prop="remark" />
        <el-table-column label="操作">
          <template #default="">
            <el-button size="mini" type="text">编辑</el-button>
            <el-button size="mini" type="text">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
      <!-- 切换分页 -->
      <div class="block">
        <!-- <span class="demonstration">完整功能</span> -->
        <el-pagination
          :page-size="params.pageSize"
          layout="total, prev, pager, next"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </div>

    </div>

    <!-- 添加弹框 -->
    <el-dialog
      title="添加区域"
      :visible="dialogVisible"
      width="580px"
      @close="closeDialog"
      @click="dialogVisible = true"
    >

      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="rules">
          <el-form-item label="区域名称" prop="areaName">
            <el-input v-model="addForm.areaName" />
          </el-form-item>
          <el-form-item label="车位数" prop="spaceNumber">
            <el-input v-model="addForm.spaceNumber" />
          </el-form-item>
          <el-form-item label="面积(m²)" prop="areaProportion">
            <el-input v-model="addForm.areaProportion" />
          </el-form-item>
          <el-form-item label="关联计费规则" prop="ruleName">
            <el-select v-model="addForm.ruleId" placeholder="请选择关联计费规则">
              <el-option
                v-for="item in ruleNameList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="addForm.remark"
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
            />
          </el-form-item>

        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button
          size="mini"
          type="primary"
          @click="createArea"
        >确 定</el-button>
      </template>

    </el-dialog>

  </div>
</template>
<script>
import { getAreaAPI, createAreaAPI, getAreaListAPI } from '@/api/carArea'
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10
      },
      areaList: [],
      total: 0,
      addForm: {
        areaName: '',
        areaProportion: null,
        ruleId: '',
        remark: '',
        id: null,
        spaceNumber: null
      },
      ruleNameList: [
        { value: 1, label: '分段计费' },
        { value: 2, label: '按次收费' },
        { value: 3, label: '按小时计费' },
        { value: 4, label: '按分钟计费' }
      ],
      rules: {
        areaName: [
          { required: true, message: '请输入区域名称!', trigger: 'blur' }
        ],
        spaceNumber: [
          { required: true, message: '请输入车位数！', trigger: 'blur' }
        ],
        areaProportion: [
          { required: true, message: '请输入面积', trigger: 'blur' }
        ],
        ruleId: [
          { required: true, message: '请选择关联计费规则', trigger: 'change' }
        ]
      },
      dialogVisible: false
    }
  },
  mounted() {
    this.getAreaList()
    this.getAreaList1()
  },
  // 获取区域管理列表
  methods: {
    async getAreaList() {
      const res = await getAreaAPI(this.params)
      console.log(res)
      this.areaList = res.data.rows
      this.total = res.data.total
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.params.page = val
      this.getAreaList()
    },
    addArea() {
      this.dialogVisible = true
    },
    // 关闭添加
    closeDialog() {
      this.dialogVisible = false
      this.addForm = {}
      this.$refs.addForm.resetFields()
    },
    // 点击确定创建区域
    async createArea() {
      await createAreaAPI(this.addForm)
      this.getAreaList()
    },
    // 获取区域下拉列表
    async getAreaList1() {
      const res = await getAreaListAPI()
      console.log(res)
    }

  }
}
</script>
